<?php

namespace app\admin\controller;

use app\lib\enum\Status;
use think\Exception;
use think\Request;

class Admin extends BaseController {
    //管理员登陆页面
    public function login() {
        if (isLogin()) {
            $this->redirect("admin/index/index");
        }
        if (request()->isAjax()) {
            $captcha = input("post.captcha");
            if (!captcha_check($captcha)) {
                $this->error("验证码错误");
            }
            $data = [
                "adm_username" => input("post.adm_username"),
                "adm_password" => md5(input("post.adm_password")),
            ];
            $result = model("CmsAdmin")->login($data);
            $this->uniteResponse($result, "登陆成功");
        }
//        $this->assign("webInfo",cache("webInfo"));
        return view();
    }

    //管理员退出登陆
    public function loginOut() {
        session("adminInfo", null);
        if (session("?adminInfo")) {
            $this->error("退出失败");
        } else {
            $this->success("退出成功", 'admin/admin/login');
        }
    }

    //管理员基本资料显示与修改
    public function baseInfo() {
        if (request()->isAjax()) {
            $data = [
                "adm_realname" => input("post.adm_realname"),
                "adm_mobile" => input("post.adm_mobile"),
                "adm_email" => input("post.adm_email")
            ];
            $result = model("CmsAdmin")->updateBaseInfo($data);
            if ($result != 1) {
                $this->error($result);
            } else {
                $this->success("信息更新成功");
            }
        }
        $adminInfo = session("adminInfo");
        $this->assign("adminInfo", $adminInfo);
        //获得管理员身份权限信息，用户页面显示改管理员身份
        $roleInfo = getAdminRoleInfo();
        $this->assign("roleInfo", $roleInfo);
        $this->assign("isInstitutionAdmin",isInstitutionAdmin());
        return view();
    }

    //修改账号密码
    public function changePass() {
        if (request()->isAjax()) {
            $data = Request::instance()->param();
            if (md5($data['oldPassword']) != session("adminInfo")['adm_password']) {
                $this->error("旧密码不正确");
            }
            $data['adm_password'] = md5($data['adm_password']);
            $result = model("CmsAdmin")->updateBaseInfo($data);
            if ($result != 1) {
                $this->error($result);
            } else {
                // 修改密码成功后跳转到登陆页面，重新登陆
                session("adminInfo", null);
                $this->success("密码修改成功", "admin/admin/login");
            }
        }
        return view();
    }

    //后台成员管理
    public function adminList() {
        if (!judgeRole(__METHOD__)) {
            $this->redirect("admin/index/forbidden");
        }
        if (request()->isAjax()) {
            $condition = [];
            $adm_role_id = input("get.adm_role_id");
            $adm_username = input("get.adm_username");
            $adm_realname=input("get.adm_realname");
            if ($adm_role_id != "" && $adm_role_id != -1) {
                $condition['adm_role_id'] = ['eq', $adm_role_id];
            }
            if ($adm_username != "") {
                $condition['adm_username'] = ['like', "%" . $adm_username . "%"];
            }
            if($adm_realname!=""){
                $condition['adm_realname']=['like',"%".$adm_realname."%"];
            }
            $page = input("get.page");
            $limit = input("get.limit");
            $count = count(model("CmsAdmin")->with("bindRole")->where($condition)->select());
            $data = model("CmsAdmin")->where($condition)
                ->page($page, $limit)->select();
            foreach ($data as &$value) {
                $value['rol_name'] = $value['bind_role']['rol_name'];
                $value['adm_create_time'] = date("Y-m-d", $value['adm_create_time']);
                $value['self']=session("adminInfo")['adm_id'];
            }
            return getJson($count, $data);
        }

        $roleInfo = model("CmsRole")->select();
        $this->assign("roleInfo", $roleInfo);

        $roleIdOfInstitution=config("setting.roleIdOfInstitution");
        $this->assign("roleIdOfInstitution",$roleIdOfInstitution);
        return view();
    }

    //后台成员添加
    public function adminAdd() {
        if (request()->isAjax()) {
            $data = [
                "adm_username" => input("post.adm_username"),
                "adm_password" => md5(input("post.adm_password")),
                "adm_realname" => input("post.adm_realname"),
                "adm_mobile" => input("post.adm_mobile"),
                "adm_email" => input("post.adm_email"),
                "adm_role_id" => input("post.adm_role_id"),
                "adm_status" => input("post.adm_status") == "on" ? Status::on : Status::off,
                "adm_create_time" => time(),
                "ins_name"=>input("post.ins_name"),
                "cty_id"=>input("post.cty_id")
            ];
            $result = model("CmsAdmin")->add($data);
            $this->uniteResponse($result,"管理员添加成功");
        }
        $roleList = model("CmsRole")->select();
        $this->assign("roleList", $roleList);
        //查询培训机构管理员信息
        $roleIdOfInstitution=getInstitutionRoleId();
        $this->assign("roleIdOfInstitution",$roleIdOfInstitution);
        //查询城市信息
        $cityList=model("PyCity")->select();
        $this->assign("cityList",$cityList);


        //如果是由培训机构列表过来的，就要限定管理员身份role
        $isFromInstitutionList=input("adm_role_id")!=""?true:false;
        $this->assign("isFromInstitutionList",$isFromInstitutionList);
        $institutionRoleInfo=model("CmsRole")->find(getInstitutionRoleId());
        $this->assign("institutionRoleInfo",$institutionRoleInfo);


        return view();
    }

    //后台成员编辑
    public function adminEdit() {
        $adm_id = input("get.adm_id");
        if (request()->isAjax()) {
            $adm_status = input("post.adm_status");
            $adm_status = isset($adm_status) ? Status::on : Status::off;
            $adm_password = input("adm_password");
            if ($adm_password != "") {
                $adm_password = md5($adm_password);
            } else {
                $adm_password = input("post.oldPass");
            }
            $data = [
                "adm_mobile" => input("post.adm_mobile"),
                "adm_password" => $adm_password,
                "adm_realname" => input("post.adm_realname"),
                "adm_role_id" => input("post.adm_role_id"),
                "adm_status" => $adm_status,
                "adm_email" => input("post.adm_email")
            ];
            $result = model("CmsAdmin")->updateAdminInfo($data, $adm_id);
            $this->uniteResponse($result, "信息编辑成功");
        }
        $adminInfo = model("CmsAdmin")->find($adm_id);
        $roleInfo = model("CmsRole")->select();
        $this->assign("adminInfo", $adminInfo);
        $this->assign("roleInfo", $roleInfo);
        return view();
    }

    //管理员删除
    public function adminDelete() {
        $adm_id = input("get.adm_id");
        $result = model("CmsAdmin")->where("adm_id", "=", $adm_id)->delete();
        if (!$result) {
            $this->error("删除失败");
        } else {
            $this->success("删除成功");
        }
    }



}
